/*! \file chatgui.cpp
	
	This contains the implementation of the ChatGUI class
*/

#include "chatgui.h"


	
ChatGUI::ChatGUI() :
	Container( GUI::getInstance(), true ),
	chatBar( this ),
	sp( this ),
	chatLog( &sp )
{
	
	minWidth = 150;
	minHeight = 150;
	setDefaultGUIStyle();
	setDraggable();
	setResizable();

	chatBar.setGUIStyle( 6,7,8,9 );
	chatBar.setBorder( true );
	
	sp.setDefaultGUIStyle();
	sp.setBorder( true );
	//~ sp.setResizable();
	chatLog.setGUIStyle( 6,7,8,9 );
	chatLog.setBackgroundColor( Color(0.0f,0.0f,0.0f,0.0f), Color(0.0f,0.0f,0.0f,0.0f) );
	chatLog.setPosition( 5, 5 ); // this needs to be set here because it will always be 
	// The following [commented] line is done in layout
	//~ chatLog.setWidth( sp.getWidth() - sp.getBorderWidth() * 2 - sp.getScrollbarWidth() );

	chatLog.setText( "LOOK OUT, TEXT-AREA! HERE COMES THE ALPHABET: abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz" );
	//~ chatLog.setText( "abcdefghijklmnopqrstuvwxyz" );
	
	chatBar.addListener( this );
	this->addListener( this );
	setFocus( &chatBar );
	
	setSize( 400, 450 ); // this calls layout
	setPosition( 50, 50 );
}

void ChatGUI::actionPerformed( Event e ) {
	if ( !chatBar.getText().empty() ) {
		chatLog.appendText( chatBar.getText() ); 
		chatBar.resetBuffer();
		sp.scrollToBottom();
	}
}

void ChatGUI::onScroll( Event e ) {
	sp.handleMouseWheel( e.button );
}

void ChatGUI::layout() {
	int minSize = 150;
	if ( w < minWidth ) setSize( minWidth, h );
	if ( h < minHeight ) setSize( w, minHeight );
	int widthWithoutBorder = w - borderWidth * 2;
	chatBar.setWidth( widthWithoutBorder );
	chatBar.setPosition( 5, borderWidth + 15 );
	sp.setPosition( 5, chatBar.getY() + chatBar.getHeight() + 5 );
	// This will set the size of the ScrollPanel's child also
	sp.setSize( widthWithoutBorder, h - borderWidth * 2 - chatBar.getHeight() - chatBar.getY() - 5 );
}

void ChatGUI::onSize( Event e ) {
	
	layout();
}
